package com.ruoyi.equ.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.uuid.IdUtils;

import com.ruoyi.equ.domain.EquBasic;
import com.ruoyi.equ.domain.EquInspection;
import com.ruoyi.equ.domain.EquInsplan;
import com.ruoyi.equ.service.IEquBasicService;
import com.ruoyi.equ.service.IEquInspectionService;
import com.ruoyi.equ.service.IEquInsplanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 设备送检Controller
 * 
 * @author luguanqing
 * @date 2020-10-29
 */
@RestController
@RequestMapping("/equ/inspection")
public class EquInspectionController extends BaseController
{
    @Autowired
    private IEquInspectionService equInspectionService;

    @Autowired
    private IEquBasicService equBasicService;

    @Autowired
    private IEquInsplanService equInsplanService;

    /**
     * 查询设备送检列表
     */
    @GetMapping("/list")
    public TableDataInfo list(EquInspection equInspection)
    {
        startPage();
        List<EquInspection> list = equInspectionService.selectEquInspectionList(equInspection);
        return getDataTable(list);
    }

    /**
     * 导出设备送检列表
     */
    @Log(title = "设备送检", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(EquInspection equInspection)
    {
        List<EquInspection> list = equInspectionService.selectEquInspectionList(equInspection);
        ExcelUtil<EquInspection> util = new ExcelUtil<EquInspection>(EquInspection.class);
        return util.exportExcel(list, "inspection");
    }

    /**
     * 获取设备送检详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo( @PathVariable("id") String id)
    {
        return AjaxResult.success(equInspectionService.selectEquInspectionById(id));
    }

    /**
     * 新增设备送检
     */
    @Log(title = "设备送检", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody EquInspection equInspection)
    {
        //获取送检设备编号
        String[] equNums = equInspection.getInsNum();
        String id = IdUtils.fastSimpleUUID();
        //根据送检单号获取设备基本信息
        List<EquBasic> equBasicList = equBasicService.selectEquBasicByNums(equNums);
        //批量插入送检详细信息
        List<EquInsplan> equInsplans = new ArrayList<EquInsplan>();
        for (int j = 0; j < equBasicList.size(); j++){
            EquInsplan equInsplan = new EquInsplan();
            equInsplan.setId(IdUtils.fastSimpleUUID() + j);
            equInsplan.setInsId(id);
            equInsplan.setEquNum(equBasicList.get(j).getEquNum());
            equInsplan.setEquName(equBasicList.get(j).getEquName());
            equInsplan.setEquModel(equBasicList.get(j).getEquModel());
            equInsplan.setEquInvalidity(equBasicList.get(j).getEquInvalidity());
            equInsplans.add(equInsplan);
        }
        int ret = equInsplanService.insertEquInsplans(equInsplans);
        //设备状态变更为送检
        int flag = equBasicService.updateEquBasics("3", equNums);
        //插入送检信息
        equInspection.setId(id);
        equInspection.setApproResult("1");
        equInspection.setInsState("1");
        return toAjax(equInspectionService.insertEquInspection(equInspection));
    }

    /**
     * 修改设备送检
     */
    @Log(title = "设备送检", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody EquInspection equInspection)
    {
        return toAjax(equInspectionService.updateEquInspection(equInspection));
    }

    /**
     * 删除设备送检
     */
    @Log(title = "设备送检", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(equInspectionService.deleteEquInspectionByIds(ids));
    }
}
